var a = Object.assign || function(a) {
    for (var t = 1; t < arguments.length; t++) {
        var e = arguments[t];
        for (var r in e) Object.prototype.hasOwnProperty.call(e, r) && (a[r] = e[r]);
    }
    return a;
}, t = (require("../../utils/util.js"), require("../../requests/request.js")), e = getApp();

Page({
    data: {
        summaryshow: !0,
        serialinfo: {},
        serialId: 0,
        loading: !1,
        isopenApp: !1,
        carlist: {},
        caryears: {},
        currenttab: 0,
        currentcars: {},
        enginetypes: {},
        name: "",
        hasforsales: !1,
        SDKVersion: "",
        system: "",
        pagesource: "",
        defaultcar: {
            carid: 0,
            carimage: "",
            cartitle: ""
        },
        tabcarshow: !0,
        newhidden: !0,
        reducehidden: !0,
        dealerhidden: !0,
        reduceinit: !0,
        tablist: [ {
            name: "车型",
            isloadmore: !0
        }, {
            name: "降价",
            isloadmore: !1
        }, {
            name: "资讯",
            isloadmore: !1
        }, {
            name: "经销商",
            isloadmore: !1
        } ],
        currentTabID: "0",
        openid: wx.getStorageSync("opid"),
        carcollect: [],
        iscollect: !1,
        clickstatus: !1,
        cityid: 201,
        cityname: "北京",
        menuTop: 0,
        menuFixed: !1,
        istolow: !1
    },
    lauchAppClick: function(a) {
        var t = this.compareVersion(this.data.SDKVersion, "1.6");
        console.log("是否支持打开App功能" + t), t < 0 && wx.showModal({
            content: "微信版本过低，无法打开APP。"
        });
    },
    launchAppError: function(a) {
        wx.showModal({
            content: "打开失败，未安装APP或微信版本过低。"
        });
    },
    onLoad: function(a) {
        var t = this, r = a.id, i = a.name, n = "", s = "app" == a.source;
        a.pagesource ? n = a.pagesource : s && (n = "app");
        var o = a.ver;
        this.data.serialId = r, this.data.name = i, this.setData({
            serialId: r,
            name: i,
            isopenApp: s,
            pagesource: n
        }), 1 == s && wx.getSystemInfo({
            success: function(a) {
                var e = a.system.indexOf("iOS");
                t.compareVersion(o, "8.6");
                s = e > -1, t.setData({
                    system: a.system,
                    SDKVersion: a.SDKVersion,
                    isopenApp: s
                });
            }
        }), t.data.openid && "" != t.data.openid || (t.data.openid = wx.getStorageSync("opid"), 
        t.setData({
            openid: t.data.openid
        }));
        var c = e.globalData;
        if (c.carcollect && c.carcollect.length > 0) {
            var d = t.data.iscollect, l = c.carcollect;
            d = l.indexOf(parseInt(t.data.serialId)) > -1, t.setData({
                iscollect: d,
                carcollect: l
            });
        } else t.getcarcollect();
        t.getCity();
    },
    onShow: function() {
        var a = this;
        try {
            var t = wx.createSelectorQuery();
            t.select("#affix").boundingClientRect(), t.exec(function(t) {
                t[0] && a.setData({
                    menuTop: t[0].top
                });
            });
        } catch (t) {
            a.setData({
                menuTop: 345
            });
        }
    },
    getcarcollect: function() {
        var a = this, r = wx.getStorageSync("opid");
        e.globalData.isgetcarcollect || t.getcarCollectlist(r, 1, function(t) {
            if (1 == t.statusCode) {
                for (var r = t.data.list, i = [], n = 0; n < r.length; n++) i.push(r[n].SerialId);
                e.globalData.carcollect = i, e.globalData.isgetcarcollect = !0;
                var s = a.data.iscollect;
                s = i.indexOf(parseInt(a.data.serialId)) > -1, a.setData({
                    iscollect: s,
                    carcollect: i
                });
            }
        }, function(a) {
            wx.showModal({
                content: "系统繁忙，请稍后再试"
            });
        }, function(a) {
            wx.hideNavigationBarLoading();
        });
    },
    onReady: function() {
        var a = this;
        a.getSalecarsbycsid();
        try {
            a.city = a.selectComponent("#city"), a.reduction = a.selectComponent("#reduction"), 
            a.news = a.selectComponent("#news"), a.dealer = a.selectComponent("#dealer");
        } catch (t) {
            a.data.istolow = !0;
        }
    },
    onShareAppMessage: function() {
        var a = this;
        return {
            title: "【" + a.data.name + "】最新报价|参数配置|图片",
            path: "/pages/summary/summary?id=" + a.data.serialId + "&name=" + a.data.name + "&pagesource=wxshare"
        };
    },
    getSalecarsbycsid: function() {
        var a = this;
        t.getSalecarsbycsid(this.data.serialId, function(t) {
            2 == t.Status && a.setData({
                "defaultcar.carid": t.Data.CarId
            }), a.loadSummaryData();
        }, null, function() {
            a.setData({
                loading: !1
            });
        });
    },
    loadSummaryData: function() {
        var a = this;
        wx.setNavigationBarTitle({
            title: this.data.name
        }), a.setData({
            loading: !0
        }), t.getSerailDetail(this.data.serialId, function(t) {
            t[0].CoverPhoto ? t[0].CoverPhoto = t[0].CoverPhoto.replace("{0}", "3") : t[0].CoverPhoto = "http://res.app.yiche.com/wechatapp/async750x420.png";
            var e = t[0].GfImgNum + t[0].KjImgNum + t[0].NsImgNum + t[0].TjImgNum + t[0].WgImgNum;
            t[0].TotalPic = isNaN(e) ? 0 : e;
            var r = t[0].ReferPrice.split("-");
            r && r.length > 1 && (r[0] == r[1] ? t[0].ReferPrice = r[0] : t[0].ReferPrice = r[0].replace("万", "") + "-" + r[1]), 
            a.setData({
                serialinfo: t[0]
            });
        }, null, function() {
            a.setData({
                loading: !1
            });
        }), a.getCarList();
    },
    getCarList: function() {
        var a = this, e = this;
        t.getCarList(this.data.serialId, function(t) {
            for (var r = [], i = [], n = 0; n < t.length; n++) 2 == t[n].SaleStatus ? t[n].Car_YearType = "未上市" : t[n].Car_YearType = t[n].Car_YearType + "款", 
            r.contains(t[n].Car_YearType) || r.push(t[n].Car_YearType), t[n].Mali = Math.round(1e3 * t[n].Power / 735), 
            a.data.defaultcar.carid == t[n].Car_ID && a.setData({
                "defaultcar.carimage": t[n].CarImg,
                "defaultcar.cartitle": t[n].Cs_ShowName + " " + t[n].Car_Name
            });
            var s = !1, i = [];
            r.indexOf("未上市") > -1 && (s = !0, r.splice(r.indexOf("未上市"), 1), (i = r.sort().reverse()).push("未上市")), 
            e.setData({
                carlist: t,
                caryears: s ? i : r.sort().reverse()
            }), a.getCurrentCars();
        }, null, function() {
            e.setData({
                loading: !1
            });
        });
    },
    onPullDownRefresh: function() {
        this.loadSummaryData(), wx.stopPullDownRefresh();
    },
    bindJumpImg: function(a) {
        var t = a.currentTarget.dataset.id;
        wx.navigateTo({
            url: "../picture/picture?serialid=" + t
        });
    },
    bindJump: function(a) {
        var t = "../price/price?carid=" + a.currentTarget.dataset.carid + "&carname=" + a.currentTarget.dataset.carname + "&showname=" + a.currentTarget.dataset.showname + "&img=" + a.currentTarget.dataset.img;
        wx.navigateTo({
            url: t
        });
    },
    bindJumpPeizhi: function(a) {
        var t = "../peizhi/peizhi?carid=" + a.currentTarget.dataset.carid + "&carname=" + a.currentTarget.dataset.carname + "&showname=" + a.currentTarget.dataset.showname + "&img=" + a.currentTarget.dataset.img;
        wx.navigateTo({
            url: t
        });
    },
    bindTab: function(a) {
        var t = a.currentTarget.dataset.index;
        this.setData({
            currenttab: t
        }), this.getCurrentCars();
    },
    daikuanclick: function(a) {
        var t = a.currentTarget.dataset.carid, e = a.currentTarget.dataset.carname, r = a.currentTarget.dataset.img;
        e = this.data.name + " " + e, wx.navigateTo({
            url: "../xunjia/xunjia?carid=" + t + "&cityid=" + this.data.cityid + "&dealerid=0&img=" + r + "&carname=" + e + "&subordertype=2&dealername=''"
        });
    },
    daiKuanGolb: function(a) {
        var t = a.currentTarget.dataset.caridext, e = a.currentTarget.dataset.cartitleext, r = a.currentTarget.dataset.carimageext;
        wx.navigateTo({
            url: "../enquire/enquire?carid=" + t + "&cityid=" + this.data.cityid + "&dealerid=0&img=" + r + "&carname=" + e + "&subordertype=2&cityname=" + this.data.cityname + "&serialid=" + this.data.serialId
        });
    },
    order: function(a) {
        var t = a.currentTarget.dataset.carid, e = a.currentTarget.dataset.carname, r = a.currentTarget.dataset.img;
        e = this.data.name + " " + e, wx.navigateTo({
            url: "../xunjia/xunjia?carid=" + t + "&cityid=" + this.data.cityid + "&dealerid=0&img=" + r + "&carname=" + e + "&subordertype=0&dealername=''"
        });
    },
    orderext: function(a) {
        var t = a.currentTarget.dataset.caridext, e = a.currentTarget.dataset.cartitleext, r = a.currentTarget.dataset.carimageext;
        wx.navigateTo({
            url: "../enquire/enquire?carid=" + t + "&cityid=" + this.data.cityid + "&dealerid=0&img=" + r + "&carname=" + e + "&subordertype=0&cityname=" + this.data.cityname + "&serialid=" + this.data.serialId
        });
    },
    returnBtn: function() {
        e.gotoIndex();
    },
    compareVersion: function(a, t) {
        if (!a) return -1;
        if (!t) return -1;
        "string" != typeof a && (a = a.toString()), "string" != typeof t && (t = t.toString()), 
        a = a.split("."), t = t.split(".");
        for (var e = Math.max(a.length, t.length); a.length < e; ) a.push("0");
        for (;t.length < e; ) t.push("0");
        for (var r = 0; r < e; r++) {
            var i = parseInt(a[r]), n = parseInt(t[r]);
            if (i > n) return 1;
            if (i < n) return -1;
        }
        return 0;
    },
    clickcarcollect: function() {
        var a = this;
        if (!a.data.openid || a.data.openid.length < 1) return wx.showModal({
            content: "缺少账号信息，不能操作"
        }), void (a.data.openid && "" != a.data.openid || (a.data.openid = wx.getStorageSync("opid"), 
        a.setData({
            openid: a.data.openid
        })));
        a.data.clickstatus || (a.setData({
            clickstatus: !0
        }), wx.showNavigationBarLoading(), a.data.iscollect ? t.deletecarCollect(a.data.openid, a.data.serialId, function(t) {
            if (1 == t.statusCode) {
                var r = a.data.carcollect;
                r || (r = []);
                var i = r.indexOf(parseInt(a.data.serialId));
                i > -1 && r.splice(i, 1), e.globalData.carcollect = r, a.setData({
                    carcollect: r,
                    iscollect: !1
                }), wx.showToast({
                    title: "已取消收藏"
                });
            } else wx.showToast({
                title: "取消失败,请稍后再试",
                icon: "none"
            });
        }, function(a) {}, function(t) {
            wx.hideNavigationBarLoading(), a.setData({
                clickstatus: !1
            });
        }) : t.addcarCollect(a.data.openid, a.data.serialId, function(t) {
            if (1 == t.statusCode) {
                var r = a.data.carcollect;
                r || (r = []), r.push(parseInt(a.data.serialId)), e.globalData.carcollect = r, a.setData({
                    carcollect: r,
                    iscollect: !0
                }), wx.showToast({
                    title: "收藏成功"
                });
            } else wx.showToast({
                title: t.message,
                icon: "none"
            });
        }, function(a) {}, function(t) {
            wx.hideNavigationBarLoading(), a.setData({
                clickstatus: !1
            });
        }));
    },
    getCurrentCars: function() {
        for (var a = this.data.caryears[this.data.currenttab], t = [], e = [], r = [], i = 0; i < this.data.carlist.length; i++) if (this.data.carlist[i].Car_YearType == a) {
            var n = this.data.carlist[i], s = "";
            n.Mali && n.Mali > 0 && (s += n.Mali + "马力"), n.UnderPan_ForwardGearNum && n.UnderPan_ForwardGearNum > 0 && (s += n.UnderPan_ForwardGearNum + "档"), 
            n.UnderPan_TransmissionType && "" != n.UnderPan_TransmissionType && (s += n.UnderPan_TransmissionType), 
            n.maliDesc = s, t.push(n);
            var o = n.Engine_ExhaustForFloat + "L/" + n.Power + "kW " + n.Engine_AddPressType, c = "";
            n.Engine_ExhaustForFloat && "" != n.Engine_ExhaustForFloat && n.Engine_ExhaustForFloat > 0 && (c += n.Engine_ExhaustForFloat + "L"), 
            n.Power > 0 && (c += "" != c ? "/" + n.Power + "kW " : n.Power + "kW "), "" != n.Engine_AddPressType && "无" != n.Engine_AddPressType && (c += n.Engine_AddPressType);
            var d = {
                Key: o,
                Value: c.replace("undefined", ""),
                Exhause: parseFloat(n.Engine_ExhaustForFloat),
                Power: parseInt(n.Power)
            };
            r.contains(d.Key) || (r.push(d.Key), e.push(d));
        }
        this.setData({
            currentcars: t.sort(function(a, t) {
                return a.CarReferPrice.replace("万", "") - t.CarReferPrice.replace("万", "");
            }),
            enginetypes: e.sort(function(a, t) {
                return a.Exhause == t.Exhause ? a.Power - t.Power : a.Exhause - t.Exhause;
            })
        });
    },
    getCity: function() {
        var a = e.globalData.locateCityInfo.id, t = e.globalData.locateCityInfo.name, r = e.globalData.selectCityParams.id, i = e.globalData.selectCityParams.name;
        r && (a = r), i && (t = i), this.setData({
            cityid: a,
            cityname: t
        });
    },
    selectCity: function() {
        var a = {
            id: this.data.cityid,
            name: this.data.cityname
        };
        this.data.istolow ? wx.showModal({
            title: " 提示",
            content: "您微信版本过低，无法切换城市"
        }) : (this.city.showCity(a), this.setData({
            summaryshow: !this.data.summaryshow
        }));
    },
    onCallBackCity: function(t) {
        this.city.hideCity();
        var r = [];
        r.id = t.detail.id, r.name = t.detail.name, e.setSelectCityParam(r);
        var i = this.data.tablist;
        i[1].isloadmore = !1, i[3].isloadmore = !1, this.setData(a({
            reduceinit: !0,
            cityid: t.detail.id,
            cityname: t.detail.name,
            summaryshow: !this.data.summaryshow
        }, i)), this.setTabData(this.data.currentTabID);
    },
    goOil: function() {
        var a = this.data.serialId;
        wx.navigateTo({
            url: "../oilwear/oilwear?serialid=" + a
        });
    },
    setTabData: function(t) {
        var e = this;
        if (0 == t) e.setData({
            tabcarshow: !0,
            newhidden: !0,
            dealerhidden: !0,
            reducehidden: !0
        }); else {
            e.setData({
                tabcarshow: !1
            });
            var r = e.data.tablist, i = e.data.tablist[t].isloadmore;
            1 == t ? (i || (e.reduction.showReduction(e.data.cityid, e.data.serialId), r[t].isloadmore = !0, 
            e.setData({
                reduceinit: !1
            })), e.setData(a({
                newhidden: !0,
                dealerhidden: !0,
                reducehidden: !1
            }, r))) : 2 == t ? (i || (e.news.getNews(), r[t].isloadmore = !0), e.setData(a({
                newhidden: !1,
                dealerhidden: !0,
                reducehidden: !0
            }, r))) : 3 == t && (i || (e.dealer.showDealer(e.data.serialId, e.data.cityid, e.data.defaultcar), 
            r[t].isloadmore = !0), e.setData(a({
                newhidden: !0,
                dealerhidden: !1,
                reducehidden: !0
            }, r)));
        }
    },
    changeTab: function(a) {
        var t = a.currentTarget.dataset.index, e = this;
        if (t != e.data.currentTabID) {
            try {
                e.setTabData(t);
            } catch (a) {
                wx.showModal({
                    title: " 提示",
                    content: "您微信版本过低，请升级版本体验该功能"
                });
            }
            e.setData({
                currentTabID: t
            });
        }
    },
    onPageScroll: function(a) {
        var t = this, e = !1;
        a.scrollTop > t.data.menuTop && (e = !0), t.setData({
            menuFixed: e
        });
    },
    onReachBottom: function(a) {
        var t = this;
        1 == t.data.currentTabID && t.reduction.loadmore(), 2 == t.data.currentTabID && t.news.loadmore();
    }
}), Array.prototype.contains = function(a) {
    return RegExp(a).test(this);
};